package repository

import (
	"boy-go/modules/system/model"
	"boy-go/modules/system/vo"
	"boy-go/pkg/db"
	"boy-go/pkg/request"
	"gorm.io/gorm"
	"time"
)

type SysDictDataRepository struct {
	DB *gorm.DB
}

func NewSysDictDataRepository() *SysDictDataRepository {
	return &SysDictDataRepository{
		DB: db.GetMasterDB(),
	}
}
func (r *SysDictDataRepository) GetById(dictCode int64) (*model.SysDictData, error) {
	db := r.DB
	m := &model.SysDictData{}
	err := db.Model(&model.SysDictData{}).Where("dict_code = ?", dictCode).First(m).Error
	if err != nil {
		return nil, err
	}
	return m, nil
}

func (r *SysDictDataRepository) List(query *vo.QuerySysDictDataReq, pageReq *request.PageRequest) ([]*model.SysDictData, int64, error) {
	tx := r.DB.Model(&model.SysDictData{})
	if query != nil {
		if query.DictSort != nil {
			tx = tx.Where("dict_sort = ?", *query.DictSort)
		}
		if query.DictLabel != nil {
			tx = tx.Where("dict_label = ?", *query.DictLabel)
		}
		if query.DictValue != nil {
			tx = tx.Where("dict_value = ?", *query.DictValue)
		}
		if query.DictType != nil {
			tx = tx.Where("dict_type = ?", *query.DictType)
		}
		if query.CssClass != nil {
			tx = tx.Where("css_class = ?", *query.CssClass)
		}
		if query.ListClass != nil {
			tx = tx.Where("list_class = ?", *query.ListClass)
		}
		if query.IsDefault != nil {
			tx = tx.Where("is_default = ?", *query.IsDefault)
		}
		if query.Status != nil {
			tx = tx.Where("status = ?", *query.Status)
		}
		if query.Remark != nil {
			tx = tx.Where("remark = ?", *query.Remark)
		}
	}
	list := make([]*model.SysDictData, 0)
	if pageReq != nil {
		offSet := 0
		limit := 10
		if query != nil {
			if pageReq.PageNum > 0 {
				offSet = (pageReq.PageNum - 1) * limit
			}
			if pageReq.PageSize > 0 {
				limit = pageReq.PageSize
			}
		}
		var total int64
		if err := tx.Count(&total).Error; err != nil {
			return nil, 0, err
		}
		if total > 0 {
			if err := tx.Select("*").Offset(offSet).Limit(limit).Find(&list).Error; err != nil {
				return nil, total, err
			}
		}
		return list, total, nil
	} else {
		if err := tx.Select("*").Find(&list).Error; err != nil {
			return nil, 0, err
		}
	}
	return list, int64(len(list)), nil
}

// Save 添加
func (r *SysDictDataRepository) Save(m *model.SysDictData) error {
	currentTime := time.Now()
	m.CreateAt = &currentTime
	return r.DB.Model(&model.SysDictData{}).Create(m).Error
}

// Update 更新
func (r *SysDictDataRepository) Update(m *model.SysDictData) error {
	currentTime := time.Now()
	m.UpdateAt = &currentTime
	return r.DB.Model(&model.SysDictData{}).Where("dict_code = ?", m.DictCode).Omit("dict_code").Updates(m).Error
}

// Delete 删除
func (r *SysDictDataRepository) Delete(dictCode int64) error {
	return r.DB.Model(&model.SysDictData{}).Where("dict_code = ?", dictCode).Delete(&model.SysDictData{}).Error
}

// DeleteBatch 批量删除
func (r *SysDictDataRepository) BatchDelete(ids []string) error {
	db := r.DB
	db = db.Where("(dict_code) IN ?", ids)
	return db.Delete(&model.SysDictData{}).Error
}

func (r *SysDictDataRepository) SelectDictDataByType(dictType string) ([]*model.SysDictData, error) {
	tx := r.DB.Model(&model.SysDictData{}).Where("dict_type = ?", dictType).Where("status = ?", "0")
	list := make([]*model.SysDictData, 0)
	if err := tx.Select("*").Order("dict_sort asc").Find(&list).Error; err != nil {
		return nil, err
	}
	return list, nil
}
